package top.lyc829.zzl_android.ui.team.my_team;

import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.google.gson.Gson;

import java.io.IOException;
import java.util.List;

import okhttp3.Response;
import top.lyc829.zzl_android.logic.bean.ActivityBean;
import top.lyc829.zzl_android.logic.bean.MyTeamInfoBean;
import top.lyc829.zzl_android.logic.bean.ResultBean;
import top.lyc829.zzl_android.logic.bean.TravelDetailBean;
import top.lyc829.zzl_android.logic.bean.UserInfoBean;
import top.lyc829.zzl_android.logic.network.Network;
import top.lyc829.zzl_android.logic.util.HttpUtil;
import top.lyc829.zzl_android.logic.util.Utils;
import top.lyc829.zzl_android.logic.util.gson.JsonUtilNew;

/**
 * @Author lyc
 * @create 2021-03-28 16:18
 */
public class MyTeamViewModel extends ViewModel {
    private MutableLiveData<Integer> teamId;
    private MutableLiveData<List<ActivityBean>> activitiesLivaData;

    private MutableLiveData<MyTeamInfoBean> teamInfoBeanLiveData;

    private MutableLiveData<List<UserInfoBean>> peopleLiveData;

    private MutableLiveData<TravelDetailBean> travelDetailLiveData;

    public MutableLiveData<TravelDetailBean> getTravelDetailLiveData() {
        if (travelDetailLiveData == null) {
            travelDetailLiveData = new MutableLiveData<>();
        }
        return travelDetailLiveData;
    }


    public MutableLiveData<List<UserInfoBean>> getPeopleLiveData() {
        if (peopleLiveData == null) {
            peopleLiveData = new MutableLiveData<>();
        }
        return peopleLiveData;
    }

    public MutableLiveData<MyTeamInfoBean> getTeamInfoBeanLiveData() {
        if (teamInfoBeanLiveData == null) {
            teamInfoBeanLiveData = new MutableLiveData<>();
        }
        return teamInfoBeanLiveData;
    }

    public MutableLiveData<Integer> getTeamId() {
        if (teamId == null) {
            teamId = new MutableLiveData<>();
        }
        return teamId;
    }

    public MutableLiveData<List<ActivityBean>> getActivitiesLivaData() {
        if (activitiesLivaData == null) {
            activitiesLivaData = new MutableLiveData<>();
        }
        return activitiesLivaData;
    }

    public void getActivityInTeam(int teamId) {
        Network.getActivityInTeam(teamId, new HttpUtil.MyCallback() {
            @Override
            public void success(Response res) throws IOException {
                ResultBean<List<ActivityBean>> resultBean = JsonUtilNew
                        .fromJsonArray(res.body().string(), ActivityBean.class);

                if (resultBean.getCode().equals(200)) {
                    getActivitiesLivaData().postValue(resultBean.getData());
                } else {
                    failed(new IOException(resultBean.getMsg()));
                }
            }

            @Override
            public void failed(IOException e) {
                Utils.Sout(e.getMessage());
            }
        });
    }

    /**
     * 获取我的团队基本信息
     */
    public void getMyTeamInfo(int teamId) {
        Network.getMyTeamInfo(teamId, new HttpUtil.MyCallback() {
            @Override
            public void success(Response res) throws IOException {
                ResultBean<MyTeamInfoBean> resultBean = JsonUtilNew
                        .fromJsonObject(res.body().string(), MyTeamInfoBean.class);

                if (resultBean.getCode().equals(200)) {
                    getTeamInfoBeanLiveData().postValue(resultBean.getData());
                } else {
                    failed(new IOException(resultBean.getMsg()));
                }
            }

            @Override
            public void failed(IOException e) {

            }
        });
    }

    public void getPeopleFromServer(int teamId) {
        Network.getTeamUsers(teamId, new HttpUtil.MyCallback() {
            @Override
            public void success(Response res) throws IOException {
                ResultBean<List<UserInfoBean>> resultBean = JsonUtilNew
                        .fromJsonArray(res.body().string(), UserInfoBean.class);

                if (resultBean.getCode().equals(200)) {
                    getPeopleLiveData().postValue(resultBean.getData());
                } else {
                    failed(new IOException(resultBean.getMsg()));
                }
            }

            @Override
            public void failed(IOException e) {
                Utils.Sout(e.getMessage());
            }
        });
    }

    public void getPeopleInActivity(int teamId, int activityId) {
        Network.getTeamUsersInActivity(teamId, activityId, new HttpUtil.MyCallback() {
            @Override
            public void success(Response res) throws IOException {
                ResultBean<List<UserInfoBean>> resultBean = JsonUtilNew
                        .fromJsonArray(res.body().string(), UserInfoBean.class);

                if (resultBean.getCode().equals(200)) {
                    getPeopleLiveData().postValue(resultBean.getData());
                } else if (resultBean.getCode().equals(371)) {
                    getPeopleLiveData().postValue(null);
                } else {
                    failed(new IOException(resultBean.getMsg()));
                }
            }

            @Override
            public void failed(IOException e) {
                Utils.Sout(e.getMessage());
            }
        });
    }

    public void loadTravelDetail(int travelId) {
        Network.loadTravelDetail(travelId, new HttpUtil.MyCallback() {
            @Override
            public void success(Response res) throws IOException {

                String resStr = res.body().string();

                ResultBean resultBean = new Gson().fromJson(resStr, ResultBean.class);

                if (resultBean.getCode().equals(200)) {
                    ResultBean<TravelDetailBean> resultBean1 = JsonUtilNew.fromJsonObject(
                            resStr, TravelDetailBean.class);
                    getTravelDetailLiveData().postValue(resultBean1.getData());
                } else if (resultBean.getCode().equals("201")) {
                    getTravelDetailLiveData().postValue(null);
                } else {
                    failed(new IOException(resultBean.getMsg()));
                }
            }

            @Override
            public void failed(IOException e) {
                Utils.Sout(e.getMessage());
            }
        });
    }

    public void deleteTravel(int userId, int travelId) {
        Network.deleteTravel(userId, travelId, new HttpUtil.MyCallback() {
            @Override
            public void success(Response res) throws IOException {
                String resStr = res.body().string();
                ResultBean<Void> resultBean = JsonUtilNew.fromJsonObject(resStr, Void.class);
                if (resultBean.getCode().equals(200)) {
                    getTravelDetailLiveData().postValue(null);
                    Utils.showToastSync("删除成功");
                } else {
                    Utils.showToastSync("删除失败");
                    failed(new IOException(resultBean.getMsg()));
                }
            }

            @Override
            public void failed(IOException e) {
                Utils.Sout(e.getMessage());
            }
        });
    }


}
